﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MyFeer.Widget.Grid.Cell;
using System.Collections.Generic;

namespace MyFeer.Widget.Grid
{
    public class ColumnHeader
    {
        private GridViewer gridViewer;
        private bool isPresssMouseLeftButton = false;
        private Point pressPos;
        private int moveColIndex;
        private bool canMove = false;
        private Line brokenLine = new Line();
        public ColumnHeader(GridViewer gridViewer)
        {
            this.gridViewer = gridViewer;
            this.gridViewer.MouseMove += new System.Windows.Input.MouseEventHandler(GridViewer_MouseMove);
            this.gridViewer.MouseLeftButtonDown += new MouseButtonEventHandler(GridViewer_MouseLeftButtonDown);
            this.gridViewer.MouseLeftButtonUp += new MouseButtonEventHandler(gridViewer_MouseLeftButtonUp);
            DoubleCollection doubleCollection = new DoubleCollection();
            doubleCollection.Add(2);
            doubleCollection.Add(4);
            doubleCollection.Add(3);
            doubleCollection.Add(2);
            this.brokenLine.StrokeDashArray = doubleCollection;
            this.brokenLine.Stroke = new SolidColorBrush(Colors.Gray);
            this.brokenLine.StrokeThickness = 1;
        }

        void gridViewer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Point mousePos = e.GetPosition(this.gridViewer.GridCanvas);
            isPresssMouseLeftButton = false;
            if (this.gridViewer.GridCanvas.Children.Contains(this.brokenLine))
            {
                this.gridViewer.GridCanvas.Children.Remove(this.brokenLine);
            }
            if (this.canMove)
            {
                int colWidth = this.gridViewer.GridModel.getColumnWidth(this.moveColIndex);
                colWidth = (int)(mousePos.X - pressPos.X + colWidth);
                this.gridViewer.GridModel.setColumnWidth(this.moveColIndex, colWidth);
                this.gridViewer.Refersh();
            }
            this.canMove = false;
        }

        void GridViewer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point mousePos = e.GetPosition(this.gridViewer.GridCanvas);
            if (mousePos.Y > GridConst.DefaultColumnHeaderHeight)
            {
                return;
            }
            pressPos = e.GetPosition(this.gridViewer.GridCanvas);
            isPresssMouseLeftButton = true;
            //if (this.canMove)
            //{
            int colWidth = this.gridViewer.GridModel.getColumnWidth(this.moveColIndex);
            double linePosX = mousePos.X;
            this.brokenLine.X1 = 0;
            this.brokenLine.X2 = 0;
            this.brokenLine.Y1 = 0;
            this.brokenLine.Y2 = this.gridViewer.GridCanvas.RenderSize.Height;
            this.brokenLine.Width = 2;
            this.brokenLine.Height = this.gridViewer.GridCanvas.RenderSize.Height;
            Canvas.SetLeft(this.brokenLine, linePosX);
            Canvas.SetTop(this.brokenLine, 0);
            this.gridViewer.GridCanvas.Children.Add(this.brokenLine);
            //}
        }

        void GridViewer_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            gridViewer.Cursor = Cursors.Arrow;
            Point mousePos = e.GetPosition(this.gridViewer.GridCanvas);
            if (mousePos.Y > GridConst.DefaultColumnHeaderHeight)
            {
                return;
            }
            if (this.isPresssMouseLeftButton && this.canMove)
            {
                gridViewer.Cursor = Cursors.SizeWE;
                int colWidth = this.gridViewer.GridModel.getColumnWidth(this.moveColIndex);
                double linePosX = mousePos.X;
                if (pressPos.X - mousePos.X > (colWidth - 2))
                {
                    linePosX = pressPos.X - colWidth + 2;
                }
                Canvas.SetLeft(this.brokenLine, linePosX);
                Canvas.SetTop(this.brokenLine, 0);
            }
            else
            {

                List<int> visibleColumnIndexs = this.gridViewer.VisibleColumnIndexs;
                int colPosX = 0;
                for (int i = 0; i < visibleColumnIndexs.Count; i++)
                {
                    int colIndex = visibleColumnIndexs[i];
                    int colWidth = this.gridViewer.GridModel.getColumnWidth(colIndex);
                    colPosX += colWidth;
                    if (Math.Abs(colPosX - (mousePos.X - GridConst.DefaultRowHeaderWidth)) < 2)
                    {
                        gridViewer.Cursor = Cursors.SizeWE;
                        moveColIndex = colIndex;
                        canMove = true;
                    }
                }
            }

        }
        public void render(int colIndex, int colPos, int colWidth)
        {
            Canvas mainCanvas = this.gridViewer.MainCanvas;
            SolidColorBrush scb = new SolidColorBrush(Colors.Black);

            CellModel cellModel = new CellModel();
            cellModel.CellStyle = GridConst.HeaderCellStyle;
            CellItem cellItem = new CellItem(this.gridViewer, cellModel);
            cellItem.render(colPos, 0, colWidth,GridConst.DefaultColumnHeaderHeight);

            TextBlock cotentControl = new TextBlock()
            {
                Height = GridConst.DefaultColumnHeaderHeight,
                Width = colWidth,
                Text = ColTitleTransformer.GetColTitleString(colIndex+1),
                TextAlignment=TextAlignment.Center
            };
            Canvas.SetTop(cotentControl, 0);
            Canvas.SetLeft(cotentControl, colPos);
            mainCanvas.Children.Add(cotentControl);
        }
    }
}
